package marvin.client.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;

public class TReader extends Thread{

	private BufferedReader inFromServer;
	private boolean run;
	private SubscriptionClient subscriptionClient;

	public TReader(InputStream inputStream, SubscriptionClient subscriptionClient) {
		inFromServer = new BufferedReader(new InputStreamReader(inputStream));
		this.subscriptionClient = subscriptionClient;
	}

	public void run(){
		run = true;
		
		try {
			while (run) {
				String line = inFromServer.readLine();
				System.out.println(line);
				if(line != null){
					if(line.contains(":")){
						String[] fragments = line.split(":");
						String cmd = fragments[0];
						String value = line.substring(line.indexOf(":")+1, line.length());
						
						if(cmd.equals("sound")){
							//subscriptionClient.setSound(Integer.parseInt(value));
						} else if(cmd.equals("web")){
							subscriptionClient.createWebRadioReceiver(value);
						} else if (cmd.equals("file")){
							subscriptionClient.createFilePlayer(value);
						} else if (cmd.equals("buffer")){
							subscriptionClient.bufferWebRadioReceiver(value);
						} else if (cmd.equals("cmd")){
							if(value.equals("stop"))
								subscriptionClient.stopPlaying();
							else if(value.equals("suspend"))
								subscriptionClient.suspendPlaying();
							else if(value.equals("resume"))
								subscriptionClient.resumePlaying();
						}
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
